package com.xxmw.transaction.service.app.impl;

import com.xxmw.transaction.model.app.Hedging;
import com.xxmw.transaction.service.app.BrickService;
import com.xxmw.transaction.service.app.SymbolAggregation;
import com.xxmw.transaction.service.app.SymbolSubService;
import com.xxmw.transaction.service.app.event.SymbolAggregationPublisher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
 * 搬砖启动类
 * @since  2020/6/2 09:56
 * @author luowenliang
 */
@Slf4j
@Service("symbolAggregation")
@DependsOn(value = {"biAnTickerApplication", "biKiApplication", "bitforexApplication", "huoBiTickerApplication",
"jiuBiApplication", "okexTickerApplication", "poloniexApplication", "zBApplication"})
public class SymbolAggregationImpl implements SymbolAggregation, InitializingBean {

	@Autowired
	private BrickService brickService;

	@Autowired
	private SymbolSubService symbolSubService;


	@Override
	public void afterPropertiesSet() {
		new Thread(() -> {
			try {
				Thread.sleep(20*1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			while(true) {
				try {
					Set<String> symbolList = brickService.symbolList();
					symbolList.forEach(symbol -> {
						symbolSubService.getHedging(symbol);
					});
					TimeUnit.MICROSECONDS.sleep(100L);
				} catch (Exception e) {
					log.error("Symbol 初始化异常有可能websocket没有启动", e);
				}
			}
		}).start();
	}

}
